﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Zero.UControl.Component
{
    /// <summary>
    /// StepBar.xaml 的交互逻辑
    /// </summary>
    public partial class StepBar : UserControl
    {
        public StepBar()
        {
            InitializeComponent();
        }

        #region 依赖属性

        public ObservableCollection<StepBarItem> ItemsSource
        {
            get { return (ObservableCollection<StepBarItem>)GetValue(ItemsSourceProperty); }
            set { SetValue(ItemsSourceProperty, value); }
        }

        public static readonly DependencyProperty ItemsSourceProperty =
            DependencyProperty.Register("ItemsSource", typeof(ObservableCollection<StepBarItem>), typeof(StepBar), new PropertyMetadata(new ObservableCollection<StepBarItem>(), OnPropertyChanged));


        public StepBarItem CurrentItem
        {
            get { return (StepBarItem)GetValue(CurrentItemProperty); }
            set { SetValue(CurrentItemProperty, value); }
        }

        public static readonly DependencyProperty CurrentItemProperty =
            DependencyProperty.Register("CurrentItem", typeof(StepBarItem), typeof(StepBar), new PropertyMetadata(null));


        public int CurrentStep
        {
            get { return (int)GetValue(CurrentStepProperty); }
            set { SetValue(CurrentStepProperty, value); }
        }

        public static readonly DependencyProperty CurrentStepProperty =
            DependencyProperty.Register("CurrentStep", typeof(int), typeof(StepBar), new PropertyMetadata(0, OnCurrentStepChanged));


        public Orientation Orientation
        {
            get { return (Orientation)GetValue(OrientationProperty); }
            set { SetValue(OrientationProperty, value); }
        }

        public static readonly DependencyProperty OrientationProperty =
            DependencyProperty.Register("Orientation", typeof(Orientation), typeof(StepBar), 
                new PropertyMetadata(Orientation.Horizontal));

        #endregion

        #region 依赖方法
        private static void OnPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as StepBar)?.UpdateItemList();
        }

        private static void OnCurrentStepChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as StepBar)?.UpdateStep();
        }
        #endregion


        #region 本地方法

        private void UpdateItemList()
        {
            List<StepBarItem> steps = ItemsSource.OrderBy(item => item.Order).ToList();
            steps[steps.Count - 1].IsLastItem = true;
            NavList.ItemsSource = steps;
        }

        private void UpdateStep()
        {
            int step = CurrentStep - 1;
            if(step < 0 || step > ItemsSource.Count - 1)
            {
                throw new Exception("请检查传入的参数是否在允许的范围");
            }

            NavList.SelectedIndex = step;
        }

        public void DoPrev()
        {
            int secIdx = NavList.SelectedIndex;
            secIdx--;
            if (secIdx < 0)
                secIdx = 0;
            NavList.SelectedIndex = secIdx;
        }

        public void DoNext()
        {
            int secIdx = NavList.SelectedIndex;
            secIdx++;
            if (secIdx > NavList.Items.Count - 1)
                secIdx = NavList.Items.Count - 1;
            NavList.SelectedIndex = secIdx;
        }

        #endregion
    }

    public class StepBarItem
    {
        public int Order { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public bool IsLastItem { get; set; } = false;
    }
}
